{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import (\n",
    "    \"fmt\"\n",
    "    \"math\"\n",
    "\n",
    "    \"gonum.org/v1/gonum/mat\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create some matrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "// Create two matrices of the same size, a and b.\n",
    "a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})\n",
    "b := mat.NewDense(3, 3, []float64{8, 9, 10, 1, 4, 2, 9, 0, 2})\n",
    "c := mat.NewDense(3, 2, []float64{3, 2, 1, 4, 0, 8})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "a = ⎡1  2  3⎤\n",
       "    ⎢0  4  5⎥\n",
       "    ⎣0  0  6⎦\n",
       "\n",
       "b = ⎡ 8   9  10⎤\n",
       "    ⎢ 1   4   2⎥\n",
       "    ⎣ 9   0   2⎦\n",
       "\n"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fa := mat.Formatted(a, mat.Prefix(\"    \"))\n",
    "fb := mat.Formatted(b, mat.Prefix(\"    \"))\n",
    "fmt.Printf(\"\\na = %0.4v\\n\\n\", fa)\n",
    "fmt.Printf(\"b = %0.4v\\n\\n\", fb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Matrix addition, multiplication, powers, and apply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "d = a + b = ⎡ 9  11  13⎤\n",
       "            ⎢ 1   8   7⎥\n",
       "            ⎣ 9   0   8⎦\n",
       "\n",
       "f = a c = ⎡ 5  34⎤\n",
       "          ⎢ 4  56⎥\n",
       "          ⎣ 0  48⎦\n",
       "\n",
       "g = a^5 = ⎡        1        682  1.074e+04⎤\n",
       "          ⎢        0       1024  1.688e+04⎥\n",
       "          ⎣        0          0       7776⎦\n",
       "\n",
       "h = sqrt(a) = ⎡    1  1.414  1.732⎤\n",
       "              ⎢    0      2  2.236⎥\n",
       "              ⎣    0      0  2.449⎦\n",
       "\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Add a and b.\n",
    "d := new(mat.Dense)\n",
    "d.Add(a, b)\n",
    "fd := mat.Formatted(d, mat.Prefix(\"            \"))\n",
    "fmt.Printf(\"d = a + b = %0.4v\\n\\n\", fd)\n",
    "\n",
    "// Multiply a and c.\n",
    "f := new(mat.Dense)\n",
    "f.Mul(a, c)\n",
    "ff := mat.Formatted(f, mat.Prefix(\"          \"))\n",
    "fmt.Printf(\"f = a c = %0.4v\\n\\n\", ff)\n",
    "\n",
    "// Raising a matrix to a power.\n",
    "g := new(mat.Dense)\n",
    "g.Pow(a, 5)\n",
    "fg := mat.Formatted(g, mat.Prefix(\"          \"))\n",
    "fmt.Printf(\"g = a^5 = %0.4v\\n\\n\", fg)\n",
    "\n",
    "// Apply a function to each of the elements of a.\n",
    "h := new(mat.Dense)\n",
    "sqrt := func(_, _ int, v float64) float64 { return math.Sqrt(v) }\n",
    "h.Apply(sqrt, a)\n",
    "fh := mat.Formatted(h, mat.Prefix(\"              \"))\n",
    "fmt.Printf(\"h = sqrt(a) = %0.4v\\n\\n\", fh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go",
   "language": "go",
   "name": "gophernotes"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".go",
   "mimetype": "",
   "name": "go",
   "nbconvert_exporter": "",
   "pygments_lexer": "",
   "version": "go1.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
